﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Evaluation3.services;
using Evaluation3.classes;
public partial class uicontrol_dnTaoMoiTB : System.Web.UI.UserControl
{
    companyServices objCo = new companyServices();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Reset_Form();
        }
    }
    public remoteUser getRemoteUser() { return (Evaluation3.classes.remoteUser)Session["remoteUser"]; } 

    #region btnCancel_Click()
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Reset_Form();
    }
    #endregion

    #region Reset_Form()
    public void Reset_Form()
    {
        txtTenTB.Text = "";
        txtNamSX.Text = DateTime.Now.Year.ToString();
        txtNangSuat.Text = "";
        txtNoiSX.Text = "";
        
        btnNew.Enabled = true;
        btnNew.Visible = true;
        btnEdit.Visible = false;
        
        ddlDN.SelectedIndex = 0;
        ddlTinhTrang.SelectedIndex = 0;
        ddlDN.DataBind();
        ddlTinhTrang.DataBind();

        Bind_Grv();
    }
    #endregion
    
    #region btnNew_Click()
    protected void btnNew_Click(object sender, EventArgs e)
    {
        try
        {
            SetObjCoData();
            objCo.DN_NewEquipment();            
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            Bind_Grv();
            this.btnNew.Enabled = false;
        }
    } 
    #endregion

    #region ddlDN_SelectedIndexChanged()
    protected void ddlDN_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            Bind_Grv();
        }
        catch (Exception ex)
        {
            
            throw ex;
        }
    }
    #endregion

    #region Bind_Grv()
    public void Bind_Grv()
    {
        DataTable dt = ((DataView)dsDSTB.Select(DataSourceSelectArguments.Empty)).Table;
        grvDSTB.DataSource = dt;
        grvDSTB.DataBind();

        lblNotify.Text = "Doanh nghiệp này hiện có " + dt.Rows.Count + " thiết bị.";
    }
    #endregion
    
    #region Giấu đi các Cột không muốn ở các GridView
    protected void grvDSTB_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
        {
            //Giấu đi cột Mã Thiết bị
            e.Row.Cells[0].Visible = false;
        }
        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
        {
            //Giấu đi cột Mã Doanh nghiệp
            e.Row.Cells[5].Visible = false;
        }
    }
    #endregion

    #region grvDSTB_PageIndexChanging()
    protected void grvDSTB_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvDSTB.PageIndex = e.NewPageIndex;
        Bind_Grv();
    } 
    #endregion

    #region btnEdit_Click()
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            objCo.MaThietBi = int.Parse(Session["MaThietBi"].ToString());
            SetObjCoData();
            objCo.TB_Update();
            lblNotify.Text = "Bạn vừa cập nhật thành công thông tin một thiết bị.";

            Bind_Grv();
            Reset_Form();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    } 
    #endregion

    #region grvDSTB_RowDeleting()
    protected void grvDSTB_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Session["MaThietBi"] = grvDSTB.Rows[e.RowIndex].Cells[0].Text;
        dsDSTB.Delete();
        Bind_Grv();
    } 
    #endregion

    #region grvDSTB_RowEditing()
    protected void grvDSTB_RowEditing(object sender, GridViewEditEventArgs e)
    {
        btnNew.Visible = false;
        btnEdit.Visible = true;
        Session["MaThietBi"] = grvDSTB.Rows[e.NewEditIndex].Cells[0].Text;
        Load_FormData();
    }
    #endregion

    #region Load_FormData()
    public void Load_FormData()
    {
        try
        {
            objCo.MaThietBi = int.Parse(Session["MaThietBi"].ToString());
            DataTable dt = objCo.TB_SearchByMaTB();

            txtTenTB.Text = dt.Rows[0]["TenThietBi"].ToString();
            ddlDN.SelectedValue = dt.Rows[0]["MaDoanhNghiep"].ToString();
            txtNamSX.Text = dt.Rows[0]["NamSanXuat"].ToString();
            txtNoiSX.Text = dt.Rows[0]["NoiSanXuat"].ToString();
            txtNangSuat.Text = dt.Rows[0]["NangSuat"].ToString();
            ddlTinhTrang.SelectedValue = dt.Rows[0]["LacHau"].ToString();
        }
        catch (Exception ex)
        {
            
            throw ex;
        }
    }
    #endregion

    #region SetObjCoData()
    public void SetObjCoData()
    {
        objCo.TenThietBi = txtTenTB.Text.Trim();
        objCo.MaDoanhNghiep = int.Parse(ddlDN.SelectedValue);
        objCo.NamSanXuat = txtNamSX.Text;
        objCo.NoiSanXuat = txtNoiSX.Text;
        objCo.NangSuat = txtNangSuat.Text;
        objCo.LacHau = int.Parse(ddlTinhTrang.SelectedValue);
        objCo.MyImage = "";
    }
    #endregion
}
